<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="深入理解mybatis（二）映射文件MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。 配置文件总览1234567891011121314151617181920212223">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/深入理解mybatis（二）映射文件/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="深入理解mybatis（二）映射文件MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。 配置文件总览1234567891011121314151617181920212223">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.341Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="深入理解mybatis（二）映射文件MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。 配置文件总览1234567891011121314151617181920212223">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/深入理解mybatis（二）映射文件/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/深入理解mybatis（二）映射文件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="深入理解mybatis（二）映射文件"><a href="#深入理解mybatis（二）映射文件" class="headerlink" title="深入理解mybatis（二）映射文件"></a>深入理解mybatis（二）映射文件</h2><p>MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。</p>
<h3 id="配置文件总览"><a href="#配置文件总览" class="headerlink" title="配置文件总览"></a>配置文件总览</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span><br><span class="line"><span class="meta">&lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- mybatis中使用自动映射的文件的时候需要使用一个接口所以 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- mapper标签中的namespace 表示配置文件的接口 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- !!!!这里非常重要 注意下面语句标签的时候必须加上命名空间才是最好的这样可以防止调用重复 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"S_Mybatis.c_Mapping接口.Mymapping接口"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--映射器 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getJavaBean"</span>  <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">resultType</span>=<span class="string">"S_Mybatis.z_javabean.MyjavaBean"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 1. select 标签 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">                    -标签属性-</span></span><br><span class="line"><span class="comment">            1.id   				表示使用接口中那个方法对应这个标签,命名标准使用方法名称</span></span><br><span class="line"><span class="comment">            2.parameterType		表示传入进这个标签所使用的数据的封转集合,</span></span><br><span class="line"><span class="comment">                                (可以使用int等基本类型,如果有多个类型,需要使用java对象进行封装)</span></span><br><span class="line"><span class="comment">                                取参数使用ognl表示方法 #&#123;xx.xxx&#125;</span></span><br><span class="line"><span class="comment">            3.returntype		表示方法返回值对应的类型可以使用基本类型或者使用自定义的</span></span><br><span class="line"><span class="comment">                                javabean-(必须使在允许自动匹配的条件下)</span></span><br><span class="line"><span class="comment">            4.returnMap			使用映射集引用来作为返回值值的匹配项相当于returntype的复杂形式(不允许在自动配置的条件下)</span></span><br><span class="line"><span class="comment">            5.flushCache		将其设置为 true，任何时候只要语句被调用，都会导致本地缓存和二级缓存都会被清空，默认值：false。</span></span><br><span class="line"><span class="comment">             6.useCache			将其设置为 true，将会导致本条语句的结果被二级缓存，默认值：对 select 元素为 true。</span></span><br><span class="line"><span class="comment">             7.timeout			这个设置是在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为 unset（依赖驱动）。</span></span><br><span class="line"><span class="comment">             8.resultOrdered		这个设置仅针对嵌套结果 select 语句适用：如果为 true，就是假设包含了嵌套结果集或是分组了，</span></span><br><span class="line"><span class="comment">                                 这样的话当返回一个主结果行的时候，就不会发生有对前面结果集的引用的情况。</span></span><br><span class="line"><span class="comment">                                 这就使得在获取嵌套的结果集的时候,不至于导致内存不够用。默认值：false。</span></span><br><span class="line"><span class="comment">            9.resultSets		这个设置仅对多结果集的情况适用，它将列出语句执行后返回的结果集并每个结果集给一个名称，名称是逗号分隔的。</span></span><br><span class="line"><span class="comment">             10.statementType	告诉mybatis使用JDBC那个statement工作-STATEMENT，PREPARED 或 CALLABLE 的一个</span></span><br><span class="line"><span class="comment">                                 (statement,prepared,CallableStatement)</span></span><br><span class="line"><span class="comment">             11.resultSet		对应resultSet接口FORWARD_ONLY(允许有向前访问)，SCROLL_SENSITIVE</span></span><br><span class="line"><span class="comment">                                 (双向跟踪但是修改不会及时反映出来) 或 SCROLL_INSENSITIVE(双向修改结果及时反映出来)</span></span><br><span class="line"><span class="comment">                                 中的一个，默认值为 unset （依赖驱动）。</span></span><br><span class="line"><span class="comment">             12.databaseID		匹配数据库是指的数据库标识(在xml配置文件中使用的databaseIdPr) </span></span><br><span class="line"><span class="comment">             13.</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertJavabean"</span> &gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">            2.insert update delete标签相关属性</span></span><br><span class="line"><span class="comment">            id					命名空间中的唯一标识符，可被用来代表这条语句。</span></span><br><span class="line"><span class="comment">            parameterType		将要传入语句的参数的完全限定类名或别名。这个属性是可选的，因为 MyBatis 可以通过 TypeHandler</span></span><br><span class="line"><span class="comment">                                推断出具体传入语句的参数，默认值为 unset。</span></span><br><span class="line"><span class="comment">            parameterMap		这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。</span></span><br><span class="line"><span class="comment">            flushCache			将其设置为 true，任何时候只要语句被调用，都会导致本地缓存和二级缓存都会被清空，</span></span><br><span class="line"><span class="comment">                                默认值：true（对应插入、更新和删除语句）。</span></span><br><span class="line"><span class="comment">            timeout				这个设置是在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为 unset（依赖驱动）。</span></span><br><span class="line"><span class="comment">            statementType		STATEMENT，PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement，</span></span><br><span class="line"><span class="comment">                                PreparedStatement 或 CallableStatement，默认值：PREPARED。</span></span><br><span class="line"><span class="comment">            useGeneratedKeys	(仅对 insert 和 update 有用）这会令 MyBatis 使用 JDBC 的 getGeneratedKeys</span></span><br><span class="line"><span class="comment">                                方法来取出由数据库内部生成的主键-就是主键自动增加</span></span><br><span class="line"><span class="comment">            keyProperty			(仅对 insert 和 update 有用）这个属性是列名称表示当使用数据库自增的属性的时候</span></span><br><span class="line"><span class="comment">                                这个属性表示传入的参数对象</span></span><br><span class="line"><span class="comment">            keyColumn			(仅对 insert 和 update 有用）这个属性是数字表示第几列这个属性和上面的效果相同 - </span></span><br><span class="line"><span class="comment">            databaseId			如果配置了 databaseIdProvider，MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId</span></span><br><span class="line"><span class="comment">                                的语句；如果带或者不带的语句都有，则不带的会被忽略。		</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateJavabean"</span>&gt;</span><span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"deleteJavabean"</span>&gt;</span><span class="tag">&lt;/<span class="name">delete</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"x"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">selectKey</span>&gt;</span><span class="tag">&lt;/<span class="name">selectKey</span>&gt;</span><span class="comment">&lt;!-- 这个标签可以用来对属性属性进行定制但是不要使用因为很麻烦 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 3.sql标签-动态的声明了一个可以进行任意嵌入的代码片段 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"userColumns"</span>&gt;</span> $&#123;alias&#125;.id,$&#123;alias&#125;.username,$&#123;alias&#125;.password <span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 4.include-引入一个sql标签 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">""</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"userColumns"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 5.cache mybatis默认只是开启了一级缓存也就是说针对于同一个sqlsession才会缓存 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">cache</span>&gt;</span><span class="tag">&lt;/<span class="name">cache</span>&gt;</span><span class="comment">&lt;!-- 简单的只使用这一个标签就是开启了二级缓存,默认的配置是 --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">        默认属性：-!!!默认属性可以被select标签的usecache属性显示的表示是否使用缓存</span></span><br><span class="line"><span class="comment">        映射语句文件中的所有 select 语句将会被缓存。</span></span><br><span class="line"><span class="comment">        映射语句文件中的所有 insert,update 和 delete 语句会刷新缓存。</span></span><br><span class="line"><span class="comment">        缓存会使用 Least Recently Used(LRU,最近最少使用的)算法来收回。</span></span><br><span class="line"><span class="comment">        根据时间表(比如 no Flush Interval,没有刷新间隔), 缓存不会以任何时间顺序 来刷新。</span></span><br><span class="line"><span class="comment">        缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。</span></span><br><span class="line"><span class="comment">        缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,</span></span><br><span class="line"><span class="comment">        而且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。</span></span><br><span class="line"><span class="comment">        flushInterval(刷新间隔)可以被设置为任意的正整数,而且它们代表一个合理的毫秒形式的时间段。</span></span><br><span class="line"><span class="comment">        默认情况是不设置,也就是没有刷新间隔,</span></span><br><span class="line"><span class="comment">        缓存仅仅调用语句时刷新</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 使用默认的缓存的时候可以进行相应的配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">cache</span> <span class="attr">eviction</span>=<span class="string">"FIFO"</span> <span class="attr">flushInterval</span>=<span class="string">"60000"</span> <span class="attr">size</span>=<span class="string">"512"</span> <span class="attr">readOnly</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 开启缓存</span></span><br><span class="line"><span class="comment">        size(引用数目)	可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的 可用内存资源数目。默认值是 1024。</span></span><br><span class="line"><span class="comment">        readOnly(只读)	属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓 存对象的相同实例。</span></span><br><span class="line"><span class="comment">                        因此这些对象不能被修改。这提供了很重要的性能优势。</span></span><br><span class="line"><span class="comment">                        可读写的缓存 会返回缓存对象的拷贝(通过序列化) 。</span></span><br><span class="line"><span class="comment">                        这会慢一些,但是安全,因此默认是 false。</span></span><br><span class="line"><span class="comment">        eviction 缓存策略：</span></span><br><span class="line"><span class="comment">                        LRU – 最近最少使用的:移除最长时间不被使用的对象。</span></span><br><span class="line"><span class="comment">                        FIFO – 先进先出:按对象进入缓存的顺序来移除它们。</span></span><br><span class="line"><span class="comment">                        SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。</span></span><br><span class="line"><span class="comment">                        WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">cache</span> <span class="attr">type</span>=<span class="string">"S_Mybatis.e_自定义mybatis缓存.自定义缓存文件"</span>&gt;</span><span class="tag">&lt;/<span class="name">cache</span>&gt;</span><span class="comment">&lt;!-- 自定义缓存 --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;cache-ref namespace="com.someone.application.data.SomeMapper"/&gt; --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--参照缓存： 回想一下上一节内容, 这个特殊命名空间的唯一缓存会被使用或者刷新相同命名空间内的语句。</span></span><br><span class="line"><span class="comment">                也许将来的某个时候,你会想在命名空间中共享</span></span><br><span class="line"><span class="comment">                相同的缓存配置和实例。在这样的 情况下你可以使用 cache-ref 元素来引用另外一个缓存。</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 配置文件的参数问题</span></span><br><span class="line"><span class="comment">            使用ognl表达式可以讲表达式中的字符串动态的更换成想应的字符串类型</span></span><br><span class="line"><span class="comment">            #&#123;property,javaType=int,jdbcType=NUMERIC&#125;</span></span><br><span class="line"><span class="comment">            使用$&#123;xxx&#125;只是动态的插入一个字符串的值</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 6.resultMap-mybatis中最重要和最关键的标签 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"S_Mybatis.z_z_MyBatis实例.javabean.BeanOne"</span> <span class="attr">id</span>=<span class="string">"myid"</span> &gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">            1.resultMap标签的属性 </span></span><br><span class="line"><span class="comment">                        type-指定结果集的实现类</span></span><br><span class="line"><span class="comment">                        id-指定这个结果集的唯一id标识</span></span><br><span class="line"><span class="comment">                        automapping-是否启用自动映射</span></span><br><span class="line"><span class="comment">                        extends-表示使用</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">            2.construction </span></span><br><span class="line"><span class="comment">                    标签-当javabean中含有无参数的构造函数的时候可以使用这个方法进行构造注入</span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">constructor</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--注意当使用这个方法的时候 构造函数不能使用自动装箱的方法,也就是说int类型要写成Integer --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">idArg</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">jdbcType</span>=<span class="string">"VARCHAR"</span> /&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 使用这个方法进行构造的时候要保证参数的顺序是按照自己定义的顺序的 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">arg</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">jdbcType</span>=<span class="string">"VARCHAR"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">constructor</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- id 属性和result属性 这两个属性是相同的效果的只不过id属性表示的是主键二result表示的是一般的键 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">            3.id和result的变量属性</span></span><br><span class="line"><span class="comment">                    column-指定进行映射的列名称(就是使用mysql数据库进行查询操作最后上面出现的一列) </span></span><br><span class="line"><span class="comment">                    javaType-指定映射出来的java变量类型一般指定是基本的变量类型</span></span><br><span class="line"><span class="comment">                    property-指定需要映射的javabean的变量名称	 jdbcType-指定映射出来的数据变量类型</span></span><br><span class="line"><span class="comment">                    typeHandler-指定使用的转换器!!!必须在mybatis的配置文件中进行配置</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 使用参数注入的时候不必像构造注入的时候那样int类型必须转化成integer --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">jdbcType</span>=<span class="string">"VARCHAR"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"age"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">jdbcType</span>=<span class="string">"VARCHAR"</span> <span class="attr">property</span>=<span class="string">"age"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 4.设置一对多和一对一连级查询 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">             这两个属性可以使用嵌套模式 当使用其那套模式的时候需要进行相关的属性设置</span></span><br><span class="line"><span class="comment">            通过外链接的方法时 必须的属性！！	</span></span><br><span class="line"><span class="comment">                    property - 设置对应结果集的javabean中的参数</span></span><br><span class="line"><span class="comment">                   javatype(一对一)-</span></span><br><span class="line"><span class="comment">                   oftyp(一对多)(数据库开启了自动映射)-返回结果集的集合或者使用一个resultMap</span></span><br><span class="line"><span class="comment">           通过sql嵌套方法时 必须！！		</span></span><br><span class="line"><span class="comment">                           property - 设置对应结果集的javabean中的参数</span></span><br><span class="line"><span class="comment">                           colomn - 使用关联的列 </span></span><br><span class="line"><span class="comment">                           select - 调用的sql语句</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 一对一连级查询  pro表示类中对应的参数--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">"list"</span> &gt;</span><span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 一对多连级查询方法这个方法其实和传统的使用方法其实是没有什么区别的只不过需要将其中的属性编程一个集合</span></span><br><span class="line"><span class="comment">        (resultMap属性)并且保证关联集合中的属性名名称对应的数据库列植是通过数据库的查询出来的列值而不是自己在sql语句中定义的列值</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"list"</span> <span class="attr">column</span>=<span class="string">"x"</span> <span class="attr">resultMap</span>=<span class="string">"myid"</span>&gt;</span><span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">            property 		生成结果集中javabean对应的属性!!</span></span><br><span class="line"><span class="comment">           automapping  	自动映射 让得到的数据库列明和声明的结果集（参数名） 相同</span></span><br><span class="line"><span class="comment">           column  		进行一对多sql分层注入的时 决定使用哪一列进行判断!!</span></span><br><span class="line"><span class="comment">           columnPrefix  	允许我们在重复出现的字段名前加上一个统一的字符前缀- 一般不用</span></span><br><span class="line"><span class="comment">           fetchType 		可选的。 有效值是lazy和eager。 </span></span><br><span class="line"><span class="comment">                           如果存在，它将取代此映射的全局配置参数lazyLoadingEnabled。</span></span><br><span class="line"><span class="comment">           jdbctype  		在这个表格之前的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅仅 </span></span><br><span class="line"><span class="comment">                           需要对插入, 更新和删除操作可能为空的列进行处理。</span></span><br><span class="line"><span class="comment">                           这是 JDBC 的需要, jdbcType 而不是 MyBatis 的。</span></span><br><span class="line"><span class="comment">                           如果你直接使用 JDBC 编程,你需要指定这个类型-但 仅仅对可能为空的值。</span></span><br><span class="line"><span class="comment">           javatype		一个 Java 类的完全限定名,或一个类型别名(参考上面内建类型别名的列 表) 。</span></span><br><span class="line"><span class="comment">                           如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。</span></span><br><span class="line"><span class="comment">                           然而,如 javaType 果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的 行为。</span></span><br><span class="line"><span class="comment">           notnullcolomn 	默认情况下，仅当映射到子对象的属性的列中的至少一列非空时才创建子对象。 </span></span><br><span class="line"><span class="comment">                           使用此属性，您可以通过指定哪些列必须具有值来更改此行为，</span></span><br><span class="line"><span class="comment">                           因此只有当任何这些列不为null时，MyBatis才会创建子对象。 </span></span><br><span class="line"><span class="comment">                           可以使用逗号作为分隔符指定多个列名称。 默认值：unset。</span></span><br><span class="line"><span class="comment">           resultMap		!!</span></span><br><span class="line"><span class="comment">           resultSet</span></span><br><span class="line"><span class="comment">           typeHandle</span></span><br><span class="line"><span class="comment">           select    		!!表示用那个映射id的语句-查询的功能将会依次的进行查询先使用外层的查询语句然后在使用后面的查询语句</span></span><br><span class="line"><span class="comment">            oftype			返回的集合的每一个元素的属性</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 使用resultMap鉴别器 -鉴别器是使用在一个result中的里面的case属性的值将会使用外层定义的colum的属性的值都转化成</span></span><br><span class="line"><span class="comment">              javaTye后进行比较如果成立就会使用.里面定义的resultMap处理注意这就说明了映射的属性必须和原来的参数兼容否则就是不匹配的</span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">discriminator</span> <span class="attr">javaType</span>=<span class="string">"S_Mybatis.z_z_MyBatis实例.javabean.BeanOne"</span> <span class="attr">column</span>=<span class="string">"xxx"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">case</span> <span class="attr">value</span>=<span class="string">"1"</span> <span class="attr">resultMap</span>=<span class="string">"lll"</span>&gt;</span><span class="tag">&lt;/<span class="name">case</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">discriminator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- !!!!解决mybatis加载的性能问题 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 解决办法使用延迟加载和使用非层级加载 在mybatis 的配置文件中进行相关的配置(这个配置方法是全局性的)</span></span><br><span class="line"><span class="comment">           当不是用全局性的变量的时候可以使用在xml属性中如(select)定义的属性fetchType(eager lazy-指定使用延迟加载)</span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"S_Mybatis.z_z_MyBatis实例.javabean.BeanOne"</span> <span class="attr">id</span>=<span class="string">"lll"</span> <span class="attr">extends</span>=<span class="string">"yyy"</span>&gt;</span><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 4.Sql动态语句  ognl的标准查看截图  注意#&#123;xxx&#125;不是ognl表达式 使用方法见上面的parameter --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"xxx"</span> <span class="attr">parameterType</span>=<span class="string">"S_Mybatis.z_z_MyBatis实例.javabean.BeanOne"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">                这里使用的ognl表达式</span></span><br><span class="line"><span class="comment">        表达式的功能异常的强大他可以直接使用在变量作用域(parameterType中的yyy)中定义的变量名称别切可以调用变量的相关方法</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- if就和java中的if语句插不多但是要注意使用转义字符-具体的自己查 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"name!=null"</span>&gt;</span><span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">choose</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">when</span> <span class="attr">test</span>=<span class="string">"name=null"</span>&gt;</span><span class="tag">&lt;/<span class="name">when</span>&gt;</span><span class="comment">&lt;!-- if else-if else--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">otherwise</span>&gt;</span><span class="tag">&lt;/<span class="name">otherwise</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">choose</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">where</span>&gt;</span><span class="comment">&lt;!-- 相当于 select语句中的where  当其中的条件语句完全成立的时候才加入 where语句--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"xxx"</span>&gt;</span><span class="tag">&lt;/<span class="name">if</span>&gt;</span><span class="comment">&lt;!-- 只用当之中的语句成立的时候才会动态的添加一个where否则不会添加where语句 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">where</span>&gt;</span><span class="comment">&lt;!-- 注意表达式中的ognl表达式中#相当于ActionContext. getContext();直接使用 参数就可以了 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 通用where语句增强版 prefix-表示这段代码块的前缀,prefixOVerrides-表示填充这段代码的分割属性 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">trim</span> <span class="attr">prefix</span>=<span class="string">"where"</span> <span class="attr">prefixOverrides</span>=<span class="string">""</span> <span class="attr">suffix</span>=<span class="string">""</span> <span class="attr">suffixOverrides</span>=<span class="string">""</span>&gt;</span><span class="tag">&lt;/<span class="name">trim</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 循环语句 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">""</span> <span class="attr">close</span>=<span class="string">""</span> <span class="attr">open</span>=<span class="string">""</span>&gt;</span><span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">            collection - 一个数字或者list集合 </span></span><br><span class="line"><span class="comment">            item - 配置循环中的当前的元素</span></span><br><span class="line"><span class="comment">            index - 配置当前元素的下标</span></span><br><span class="line"><span class="comment">            open - close - 用什么元素将其中的 元素包裹起来</span></span><br><span class="line"><span class="comment">            separator - 各个元素的间隔符号</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 这个元素表示一个配置文件自己定义的上下文变量 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">bind</span> <span class="attr">name</span>=<span class="string">"xx"</span> <span class="attr">value</span>=<span class="string">"xxx"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 参数传递方法 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"hehe"</span> <span class="attr">parameterType</span>=<span class="string">"o_EhCache缓存.ceshi.Javabean"</span> &gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 使用这个表达式直接将xxx对象或者map中的属性自动的注入进来 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- parameterType="xxx.xxx"-mybaits的内置对象实现自动注入相关的属性 --&gt;</span></span><br><span class="line">        select * from xxx where name =#&#123;id&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>SQL 映射文件有很少的几个顶级元素（按照它们应该被定义的顺序）：</p>
<ul>
<li>cache – 给定命名空间的缓存配置。</li>
<li>cache-ref – 其他命名空间缓存配置的引用。</li>
<li>resultMap – 是最复杂也是最强大的元素，用来描述如何从数据库结果集中来加载对象。</li>
<li>parameterMap – 已废弃！老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除，这里不会记录。</li>
<li>sql – 可被其他语句引用的可重用语句块。</li>
<li>insert – 映射插入语句</li>
<li>update – 映射更新语句</li>
<li>delete – 映射删除语句</li>
<li>select – 映射查询语句</li>
</ul>
<h3 id="标签使用详解"><a href="#标签使用详解" class="headerlink" title="标签使用详解"></a>标签使用详解</h3><h4 id="select-insert-update-delete"><a href="#select-insert-update-delete" class="headerlink" title="select insert update delete"></a>select insert update delete</h4><p>selete标签相关的属性</p>
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>在命名空间中唯一的标识符，可以被用来引用这条语句。</td>
</tr>
<tr>
<td>parameterType</td>
<td>将会传入这条语句的参数类的完全限定名或别名。这个属性是可选的，因为 MyBatis 可以通过 TypeHandler 推断出具体传入语句的参数，默认值为 unset。</td>
</tr>
<tr>
<td>parameterMap</td>
<td>这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。</td>
</tr>
<tr>
<td>resultType</td>
<td>从这条语句中返回的期望类型的类的完全限定名或别名。注意如果是集合情形，那应该是集合可以包含的类型，而不能是集合本身。使用 resultType 或 resultMap，但不能同时使用。</td>
</tr>
<tr>
<td>resultMap</td>
<td>外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性，对其有一个很好的理解的话，许多复杂映射的情形都能迎刃而解。使用 resultMap 或 resultType，但不能同时使用。</td>
</tr>
<tr>
<td>flushCache</td>
<td>将其设置为 true，任何时候只要语句被调用，都会导致本地缓存和二级缓存都会被清空，默认值：false。</td>
</tr>
<tr>
<td>useCache</td>
<td>将其设置为 true，将会导致本条语句的结果被二级缓存，默认值：对 select 元素为 true。</td>
</tr>
<tr>
<td>timeout</td>
<td>这个设置是在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为 unset（依赖驱动）。</td>
</tr>
<tr>
<td>fetchSize</td>
<td>这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset（依赖驱动）。</td>
</tr>
<tr>
<td>statementType</td>
<td>STATEMENT，PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement，PreparedStatement 或 CallableStatement，默认值：PREPARED。</td>
</tr>
<tr>
<td>resultSetType</td>
<td>FORWARD_ONLY，SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个，默认值为 unset （依赖驱动）。</td>
</tr>
<tr>
<td>databaseId</td>
<td>如果配置了 databaseIdProvider，MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId的语句；如果带或者不带的语句都有，则不带的会被忽略。</td>
</tr>
<tr>
<td>resultOrdered</td>
<td>这个设置仅针对嵌套结果 select 语句适用：如果为 true，就是假设包含了嵌套结果集或是分组了，这样的话当返回一个主结果行的时候，就不会发生有对前面结果集的引用的情况。这就使得在获取嵌套的结果集的时候不至于导致内存不够用。默认值：false。</td>
</tr>
<tr>
<td>resultSets</td>
<td>这个设置仅对多结果集的情况适用，它将列出语句执行后返回的结果集并每个结果集给一个名称，名称是逗号分隔的。</td>
</tr>
</tbody>
</table>
<h4 id="insert-update-delete属性相关标签"><a href="#insert-update-delete属性相关标签" class="headerlink" title="insert update delete属性相关标签"></a>insert update delete属性相关标签</h4><table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>命名空间中的唯一标识符，可被用来代表这条语句。</td>
</tr>
<tr>
<td>parameterType</td>
<td>将要传入语句的参数的完全限定类名或别名。这个属性是可选的，因为 MyBatis 可以通过 TypeHandler</td>
<td>推断出具体传入语句的参数，默认值为 unset。</td>
</tr>
<tr>
<td>parameterMap</td>
<td>这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。</td>
</tr>
<tr>
<td>flushCache</td>
<td>将其设置为 true，任何时候只要语句被调用，都会导致本地缓存和二级缓存都会被清空，默认值：true（对应插入、更新和删除语句）。</td>
</tr>
<tr>
<td>timeout</td>
<td>这个设置是在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为 unset（依赖驱动）。</td>
</tr>
<tr>
<td>statementType</td>
<td>STATEMENT，PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement，PreparedStatement 或 CallableStatement，默认值：PREPARED。</td>
</tr>
<tr>
<td>useGeneratedKeys</td>
<td>（仅对 insert 和 update 有用）这会令 MyBatis 使用 JDBC 的 getGeneratedKeys</td>
<td>方法来取出由数据库内部生成的主键（比如：像 MySQL 和 SQL Server 这样的关系数据库管理系统的自动递增字段），默认值：false。</td>
</tr>
<tr>
<td>keyProperty</td>
<td>（仅对 insert 和 update 有用）唯一标记一个属性，MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey 子元素设置它的键值，默认：unset。如果希望得到多个生成的列，也可以是逗号分隔的属性名称列表。</td>
</tr>
<tr>
<td>keyColumn</td>
<td>（仅对 insert 和 update 有用）通过生成的键值设置表中的列名，这个设置仅在某些数据库（像 PostgreSQL）是必须的，当主键列不是表中的第一列的时候需要设置。如果希望得到多个生成的列，也可以是逗号分隔的属性名称列表。</td>
</tr>
<tr>
<td>databaseId</td>
<td>如果配置了 databaseIdProvider，MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句；如果带或者不带的语句都有，则不带的会被忽略。</td>
</tr>
</tbody>
</table>
<p><strong>注意</strong>：useGeneratedKeys keyProperty keyColumn parameterType主要用来进行 自增加主键的 回调操作，就是当进行插入操作的之后，自动生成的主键重新注入到传入的 parameterType对应的javabean中，（底层使用jbdc的getGeneratedKeys方法）</p>
<h4 id="selectKey-和-keyProperty-keyColumn-结合使用进行主键回填操作"><a href="#selectKey-和-keyProperty-keyColumn-结合使用进行主键回填操作" class="headerlink" title="selectKey 和 keyProperty keyColumn 结合使用进行主键回填操作"></a>selectKey 和 keyProperty keyColumn 结合使用进行主键回填操作</h4><p>在insert 和 update 标签中才会有用的一个标签   会在执行语句之前先进行一次查询，并将得到的数据放入 keyProperty属性指定名称的字段中（调用传入的变量的keyproperty指定名称的地方）作为后面进行操作的数据，一般用在 对于不支持自动生成类型的数据库或可能不支持自动生成主键 JDBC 驱动，来生成主键。</p>
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>keyProperty</td>
<td>selectKey 语句结果应该被设置的目标属性。如果希望得到多个生成的列，也可以是逗号分隔的属性名称列表。</td>
</tr>
<tr>
<td>keyColumn</td>
<td>匹配属性的返回结果集中的列名称。如果希望得到多个生成的列，也可以是逗号分隔的属性名称列表。   注意 Property和Column一一对应</td>
</tr>
<tr>
<td>resultType</td>
<td>结果的类型。MyBatis 通常可以推算出来，但是为了更加确定写上也不会有什么问题。MyBatis 允许任何简单类型用作主键的类型，包括字符串。如果希望作用于多个生成的列，则可以使用一个包含期望属性的 Object 或一个 Map。</td>
</tr>
<tr>
<td>order</td>
<td>这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE，那么它会首先选择主键，设置 keyProperty 然后执行插入语句。如果设置为 AFTER，那么先执行插入语句，然后是 selectKey 元素 – 这和像 Oracle 的数据库相似，在插入语句内部可能有嵌入索引调用。</td>
</tr>
<tr>
<td>statementType</td>
<td>与前面相同，MyBatis 支持 STATEMENT，PREPARED 和 CALLABLE 语句的映射类型，分别代表 PreparedStatement 和 CallableStatement 类型。</td>
</tr>
</tbody>
</table>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertAuthor"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">selectKey</span> <span class="attr">keyProperty</span>=<span class="string">"id"</span> <span class="attr">resultType</span>=<span class="string">"int"</span> <span class="attr">order</span>=<span class="string">"BEFORE"</span>&gt;</span></span><br><span class="line">        select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1</span><br><span class="line">    <span class="tag">&lt;/<span class="name">selectKey</span>&gt;</span></span><br><span class="line">    insert into Author</span><br><span class="line">    (id, username, password, email,bio, favourite_section)</span><br><span class="line">    values</span><br><span class="line">    (#&#123;id&#125;, #&#123;username&#125;, #&#123;password&#125;, #&#123;email&#125;, #&#123;bio&#125;, #&#123;favouriteSection,jdbcType=VARCHAR&#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="sql标签和include-标签"><a href="#sql标签和include-标签" class="headerlink" title="sql标签和include 标签"></a>sql标签和include 标签</h4><p>sql和include 标签一般进行组合使用，形成各种参数化配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"userColumns"</span>&gt;</span> $&#123;alias&#125;.id,$&#123;alias&#125;.username,$&#123;alias&#125;.password <span class="tag">&lt;/<span class="name">sql</span>&gt;</span><span class="comment">&lt;!--设置相关的各种属性--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectUsers"</span> <span class="attr">resultType</span>=<span class="string">"map"</span>&gt;</span><span class="comment">&lt;!--include标签动态的传入相关的属性--&gt;</span></span><br><span class="line">  select</span><br><span class="line">    <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"userColumns"</span>&gt;</span><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"alias"</span> <span class="attr">value</span>=<span class="string">"t1"</span>/&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span>,</span><br><span class="line">    <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"userColumns"</span>&gt;</span><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"alias"</span> <span class="attr">value</span>=<span class="string">"t2"</span>/&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">  from some_table t1</span><br><span class="line">    cross join some_table t2</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="结果集映射方法"><a href="#结果集映射方法" class="headerlink" title="结果集映射方法"></a>结果集映射方法</h4><p>查询语句</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Very Complex Statement --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectBlogDetails"</span> <span class="attr">resultMap</span>=<span class="string">"detailedBlogResultMap"</span>&gt;</span></span><br><span class="line">    select</span><br><span class="line">    B.id as blog_id,</span><br><span class="line">    B.title as blog_title,</span><br><span class="line">    B.author_id as blog_author_id,</span><br><span class="line">    A.id as author_id,</span><br><span class="line">    A.username as author_username,</span><br><span class="line">    A.password as author_password,</span><br><span class="line">    A.email as author_email,</span><br><span class="line">    A.bio as author_bio,</span><br><span class="line">    A.favourite_section as author_favourite_section,</span><br><span class="line">    P.id as post_id,</span><br><span class="line">    P.blog_id as post_blog_id,</span><br><span class="line">    P.author_id as post_author_id,</span><br><span class="line">    P.created_on as post_created_on,</span><br><span class="line">    P.section as post_section,</span><br><span class="line">    P.subject as post_subject,</span><br><span class="line">    P.draft as draft,</span><br><span class="line">    P.body as post_body,</span><br><span class="line">    C.id as comment_id,</span><br><span class="line">    C.post_id as comment_post_id,</span><br><span class="line">    C.name as comment_name,</span><br><span class="line">    C.comment as comment_text,</span><br><span class="line">    T.id as tag_id,</span><br><span class="line">    T.name as tag_name</span><br><span class="line">    from Blog B</span><br><span class="line">    left outer join Author A on B.author_id = A.id</span><br><span class="line">    left outer join Post P on B.id = P.blog_id</span><br><span class="line">    left outer join Comment C on P.id = C.post_id</span><br><span class="line">    left outer join Post_Tag PT on PT.post_id = P.id</span><br><span class="line">    left outer join Tag T on PT.tag_id = T.id</span><br><span class="line">    where B.id = #&#123;id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>结果集</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Very Complex Result Map --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"detailedBlogResultMap"</span> <span class="attr">type</span>=<span class="string">"Blog"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">constructor</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">idArg</span> <span class="attr">column</span>=<span class="string">"blog_id"</span> <span class="attr">javaType</span>=<span class="string">"int"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">constructor</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"title"</span> <span class="attr">column</span>=<span class="string">"blog_title"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">"author"</span> <span class="attr">javaType</span>=<span class="string">"Author"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"author_id"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"username"</span> <span class="attr">column</span>=<span class="string">"author_username"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"password"</span> <span class="attr">column</span>=<span class="string">"author_password"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"email"</span> <span class="attr">column</span>=<span class="string">"author_email"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"bio"</span> <span class="attr">column</span>=<span class="string">"author_bio"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"favouriteSection"</span> <span class="attr">column</span>=<span class="string">"author_favourite_section"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"posts"</span> <span class="attr">ofType</span>=<span class="string">"Post"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"post_id"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"subject"</span> <span class="attr">column</span>=<span class="string">"post_subject"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">"author"</span> <span class="attr">javaType</span>=<span class="string">"Author"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"comments"</span> <span class="attr">ofType</span>=<span class="string">"Comment"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"comment_id"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"tags"</span> <span class="attr">ofType</span>=<span class="string">"Tag"</span> &gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"tag_id"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">discriminator</span> <span class="attr">javaType</span>=<span class="string">"int"</span> <span class="attr">column</span>=<span class="string">"draft"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">case</span> <span class="attr">value</span>=<span class="string">"1"</span> <span class="attr">resultType</span>=<span class="string">"DraftPost"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">discriminator</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="resultmap标签"><a href="#resultmap标签" class="headerlink" title="resultmap标签"></a>resultmap标签</h4><blockquote>
<p>表示一个映射结果集</p>
</blockquote>
<p>相关属性：</p>
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>当前命名空间中的一个唯一标识，用于标识一个result map.</td>
</tr>
<tr>
<td>type</td>
<td>类的全限定名, 或者一个类型别名 (内置的别名可以参考上面的表格).</td>
</tr>
<tr>
<td>autoMapping</td>
<td>如果设置这个属性，MyBatis将会为这个ResultMap开启或者关闭自动映射。这个属性会覆盖全局的属性autoMappingBehavior。默认值为：unset。</td>
</tr>
</tbody>
</table>
<h4 id="resultmap标签子标签"><a href="#resultmap标签子标签" class="headerlink" title="resultmap标签子标签"></a>resultmap标签子标签</h4><p>id&amp;result设置对应结果集中类的各种参数，注意要有set方法，区别 id 表示的标识属性（一般就是表的主键）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"post_id"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"subject"</span> <span class="attr">column</span>=<span class="string">"post_subject"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>相关属性</p>
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>property</td>
<td>映射到列结果的字段或属性。如果匹配的是存在的,和给定名称相同 的 JavaBeans 的属性,那么就会使用。否则 MyBatis 将会寻找给定名称 property 的字段。这两种情形你可以使用通常点式的复杂属性导航。比如,你 可以这样映射一些东西: “username” ,或者映射到一些复杂的东西: “address.street.number” 。</td>
</tr>
<tr>
<td>column</td>
<td>从数据库中得到的列名,或者是列名的重命名标签。这也是通常和会 传递给 resultSet.getString(columnName)方法参数中相同的字符串。</td>
</tr>
<tr>
<td>javaType</td>
<td>一个 Java 类的完全限定名,或一个类型别名(参考上面内建类型别名 的列表) 。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。 然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的行为。</td>
</tr>
<tr>
<td>jdbcType</td>
<td>在这个表格之后的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅 仅需要对插入,更新和删除操作可能为空的列进行处理。这是 JDBC jdbcType 的需要,而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定 这个类型-但仅仅对可能为空的值。</td>
</tr>
<tr>
<td>typeHandler</td>
<td>我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默 认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理 器的实现,或者是类型别名。</td>
</tr>
</tbody>
</table>
<h4 id="constructor标签"><a href="#constructor标签" class="headerlink" title="constructor标签"></a>constructor标签</h4><p>constructor标签 使用构造方法进行注入，注意必须提供相对应的构造方法，参数的书写顺序和构造函数的顺序相同（3.4.3起可以不同但是要对构造函数添加@Param注解，和启用useActualParamname）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">constructor</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">idArg</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">javaType</span>=<span class="string">"int"</span> <span class="attr">name</span>=<span class="string">"id"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">arg</span> <span class="attr">column</span>=<span class="string">"age"</span> <span class="attr">javaType</span>=<span class="string">"_int"</span> <span class="attr">name</span>=<span class="string">"age"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">arg</span> <span class="attr">column</span>=<span class="string">"username"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">name</span>=<span class="string">"username"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">constructor</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>相关属性</p>
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>column</td>
<td>来自数据库的类名,或重命名的列标签。这和通常传递给 resultSet.getString(columnName)方法的字符串是相同的。</td>
</tr>
<tr>
<td>javaType</td>
<td>一个Java 类的完全限定名,或一个类型别名(参考上面内建类型别名的列表)。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。然而,如 果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的 行为。</td>
</tr>
<tr>
<td>jdbcType</td>
<td>在这个表格之前的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅仅 需要对插入, 更新和删除操作可能为空的列进行处理。这是 JDBC 的需要, jdbcType 而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定这个类型-但 仅仅对可能为空的值。</td>
</tr>
<tr>
<td>typeHandler</td>
<td>我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默认的 类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现, 或者是类型别名。</td>
</tr>
<tr>
<td>select</td>
<td>用于加载复杂类型属性的映射语句的ID,从column中检索出来的数据，将作为此select语句的参数。具体请参考Association标签。</td>
</tr>
<tr>
<td>resultMap</td>
<td>ResultMap的ID，可以将嵌套的结果集映射到一个合适的对象树中，功能和select属性相似，它可以实现将多表连接操作的结果映射成一个单一的ResultSet。这样的ResultSet将会将包含重复或部分数据重复的结果集正确的映射到嵌套的对象树中。为了实现它, MyBatis允许你 “串联” ResultMap,以便解决嵌套结果集的问题。想了解更多内容，请参考下面的Association元素。</td>
</tr>
<tr>
<td>name</td>
<td>构造方法形参的名字。通过指定具体的名字你可以以任意顺序写入arg元素。参看上面的解释。从3.4.3版本起。</td>
</tr>
</tbody>
</table>
<h4 id="cache-二级缓存"><a href="#cache-二级缓存" class="headerlink" title="cache-二级缓存"></a>cache-二级缓存</h4><p>默认情况下只是默认开启了一级缓存，也就是对同一个sqlsession缓存相关的各种数据，开起二级缓存将会在sqlsessionFactory级别进行缓存，缓存的数据所有的sqlsession共享</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">cache</span> <span class="attr">eviction</span>=<span class="string">"FIFO"</span> <span class="attr">flushInterval</span>=<span class="string">"60000"</span> <span class="attr">size</span>=<span class="string">"512"</span>  <span class="attr">readOnly</span>=<span class="string">"true"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>参数名</th>
<th>参数作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>eviction</td>
<td>回收策略，LRU – 最近最少使用的:移除最长时间不被使用的对象。 FIFO – 先进先出:按对象进入缓存的顺序来移除它们。 SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。</td>
</tr>
<tr>
<td>flushInterval(刷新间隔)</td>
<td>可以被设置为任意的正整数,而且它们代表一个合理的毫秒 形式的时间段。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。</td>
</tr>
<tr>
<td>size(引用数目)</td>
<td>可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的 可用内存资源数目。默认值是 1024</td>
</tr>
<tr>
<td>readOnly(只读)</td>
<td>属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓 存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。可读写的缓存 会返回缓存对象的拷贝(通过序列化) 。这会慢一些,但是安全,因此默认是 false。</td>
</tr>
</tbody>
</table>
<h4 id="引申mybatis使用自定义缓存"><a href="#引申mybatis使用自定义缓存" class="headerlink" title="引申mybatis使用自定义缓存"></a>引申mybatis使用自定义缓存</h4><p>除了这些自定义缓存的方式, 你也可以通过实现你自己的缓存或为其他第三方缓存方案 创建适配器来完全覆盖缓存行为。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">cache</span> <span class="attr">type</span>=<span class="string">"com.domain.something.MyCustomCache"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>这个示 例展 示了 如何 使用 一个 自定义 的缓 存实 现。type 属 性指 定的 类必 须实现 org.mybatis.cache.Cache 接口。这个接口是 MyBatis 框架中很多复杂的接口之一,但是简单 给定它做什么就行。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Cache</span> </span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">getId</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getSize</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">putObject</span><span class="params">(Object key, Object value)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">Object <span class="title">getObject</span><span class="params">(Object key)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">hasKey</span><span class="params">(Object key)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">Object <span class="title">removeObject</span><span class="params">(Object key)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>要配置你的缓存, 简单和公有的 JavaBeans 属性来配置你的缓存实现, 而且是通过 cache 元素来传递属性, 比如, 下面代码会在你的缓存实现中调用一个称为 “setCacheFile(String file)” 的方法:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">cache</span> <span class="attr">type</span>=<span class="string">"com.domain.something.MyCustomCache"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"cacheFile"</span> <span class="attr">value</span>=<span class="string">"/tmp/my-custom-cache.tmp"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">cache</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>你可以使用所有简单类型作为 JavaBeans 的属性,MyBatis 会进行转换。 And you can specify a placeholder(e.g. ${cache.file}) to replace value defined at configuration properties.</p>
<p>从3.4.2版本开始，MyBatis已经支持在所有属性设置完毕以后可以调用一个初始化方法。如果你想要使用这个特性，请在你的自定义缓存类里实现 org.apache.ibatis.builder.InitializingObject 接口。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">InitializingObject</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">initialize</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/算法阶段性小结(一)/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/详解http协议-状态码和报文信息/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#深入理解mybatis（二）映射文件"><span class="nav-number">1.</span> <span class="nav-text">深入理解mybatis（二）映射文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置文件总览"><span class="nav-number">1.1.</span> <span class="nav-text">配置文件总览</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#标签使用详解"><span class="nav-number">1.2.</span> <span class="nav-text">标签使用详解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#select-insert-update-delete"><span class="nav-number">1.2.1.</span> <span class="nav-text">select insert update delete</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#insert-update-delete属性相关标签"><span class="nav-number">1.2.2.</span> <span class="nav-text">insert update delete属性相关标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#selectKey-和-keyProperty-keyColumn-结合使用进行主键回填操作"><span class="nav-number">1.2.3.</span> <span class="nav-text">selectKey 和 keyProperty keyColumn 结合使用进行主键回填操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sql标签和include-标签"><span class="nav-number">1.2.4.</span> <span class="nav-text">sql标签和include 标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#结果集映射方法"><span class="nav-number">1.2.5.</span> <span class="nav-text">结果集映射方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#resultmap标签"><span class="nav-number">1.2.6.</span> <span class="nav-text">resultmap标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#resultmap标签子标签"><span class="nav-number">1.2.7.</span> <span class="nav-text">resultmap标签子标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#constructor标签"><span class="nav-number">1.2.8.</span> <span class="nav-text">constructor标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#cache-二级缓存"><span class="nav-number">1.2.9.</span> <span class="nav-text">cache-二级缓存</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#引申mybatis使用自定义缓存"><span class="nav-number">1.2.10.</span> <span class="nav-text">引申mybatis使用自定义缓存</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
